package com.sbtr.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sbtr.business.system.dto.DictionaryParamDto;
import com.sbtr.business.system.entity.Dictionary;
import com.sbtr.business.system.entity.DictionaryParam;
import com.sbtr.business.system.service.DictionaryParamService;
import com.sbtr.business.system.service.DictionaryService;
import com.sbtr.business.utils.AnalyzeUtil;
import com.sbtr.common.PageDto;
import com.sbtr.common.Response;
import com.sbtr.controller.common.CommonController;
import com.sbtr.exception.BDException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 具体选项字段列表 前端控制器
 *
 * @author dgl
 * @since 2023-03-23
 */
@RestController
@RequestMapping("/dictionaryParam")
@Api(tags = "数据字典具体选项字段列表")
public class DictionaryParamController extends CommonController<DictionaryParam,DictionaryParamService> {

    @Autowired
    AnalyzeUtil analyzeUtil;
    @Autowired
    DictionaryService dictionaryService;

    @GetMapping("/List")
    @ApiOperation(httpMethod = "GET", value = "列表")
    public Response<List<DictionaryParam>> List(DictionaryParam dictionaryParam){
        QueryWrapper<DictionaryParam> wrapper =  new QueryWrapper<>(dictionaryParam);
        wrapper.orderByAsc("number");
        return Response.success(dictionaryParam.selectList(wrapper));
    }

    @GetMapping("/getListByDirId")
    @ApiOperation(httpMethod = "GET", value = "根据字典id获取其选项列表")
    public Response<List<DictionaryParam>> getListByDirId(Long id){
        Dictionary dictionary = dictionaryService.getById(id);
        QueryWrapper<DictionaryParam> wrapper =  new QueryWrapper<>();
        wrapper.eq("dir_id",dictionary.getId());
        List<DictionaryParam> list = u.list(wrapper);
        return Response.success(list);
    }


    @GetMapping("/pageList")
    @ApiOperation(httpMethod = "GET", value = "具体选项字段列表分页列表")
    public Response<PageDto<DictionaryParam>> pageList(DictionaryParamDto dictionaryParamDto){
        return Response.success(u.pageList(dictionaryParamDto));
    }


    @PostMapping("/update")
    @ApiOperation(httpMethod = "POST", value = "修改")
    public Response<String> update(@RequestBody DictionaryParam dictionaryParam){
        if(dictionaryParam.getDirId()==null){
            throw new BDException("字典id不能为空");
        }
        List<DictionaryParam> list = dictionaryParam.selectList(new QueryWrapper<DictionaryParam>().eq("dir_id", dictionaryParam.getDirId()));

        if (StringUtils.isNotBlank(dictionaryParam.getOptCode())) {
            for (DictionaryParam dictionaryParam1:list) {
                if(dictionaryParam.getOptCode().equals(dictionaryParam1.getOptCode())){
                    throw new BDException("编号已经存在");
                }
            }
        }
//        if (StringUtils.isNotBlank(dictionaryParam.getOptName())) {
//            for (DictionaryParam dictionaryParam1:list) {
//                if(dictionaryParam.getOptName().equals(dictionaryParam1.getOptName())){
//                    throw new BDException("名称已经存在");
//                }
//            }
//        }

        if(!dictionaryParam.updateById()){
            throw new BDException("修改失败");
        }
        analyzeUtil.flushDictionary();
        return Response.success("修改成功");
    }

    @PostMapping("/save")
    @ApiOperation(httpMethod = "POST", value = "具体选项字段列表保存")
    public Response<DictionaryParam> save(@RequestBody DictionaryParam dictionaryParam){

        if(dictionaryParam.getDirId()==null){
            throw new BDException("字典id不能为空");
        }

        List<DictionaryParam> list = dictionaryParam.selectList(new QueryWrapper<DictionaryParam>().eq("dir_id", dictionaryParam.getDirId()));
//        if (dictionaryParam.getNumber() != null) {
//            for (DictionaryParam dictionaryParam1:list) {
//                if(dictionaryParam.getNumber().equals(dictionaryParam1.getNumber())){
//                    throw new BDException("序号已经存在");
//                }
//            }
//        }

        if (StringUtils.isNotBlank(dictionaryParam.getOptCode())) {
            for (DictionaryParam dictionaryParam1:list) {
                if(dictionaryParam.getOptCode().equals(dictionaryParam1.getOptCode())){
                    throw new BDException("编号已经存在");
                }
            }
        }
//        if (StringUtils.isNotBlank(dictionaryParam.getOptName())) {
//            for (DictionaryParam dictionaryParam1:list) {
//                if(dictionaryParam.getOptName().equals(dictionaryParam1.getOptName())){
//                    throw new BDException("名称已经存在");
//                }
//            }
//        }

        dictionaryParam.setId(null);
        if(dictionaryParam.insert()){
            analyzeUtil.flushDictionary();
            return Response.success(dictionaryParam);
        }else throw new BDException("保存失败");
    }


    @PostMapping("/deleteByIds")
    @ApiOperation(httpMethod = "POST", value = "通用接口---根据id批量删除,逗号分隔ps:'1,2,3'，关联子表删除请重写")
    public Response<String> deleteByIds(String ids){
        List<String> list = Arrays.asList(ids.split(","));
        if(!u.removeByIds(list)){
            throw new BDException("删除失败");
        }
        analyzeUtil.flushDictionary();

        return Response.success("删除成功");
    }




}

